package tman.main;

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * -------------------------
 * OverlaidBarChartDemo.java
 * -------------------------
 * (C) Copyright 2002-2004, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: OverlaidBarChartDemo.java,v 1.36 2004/06/02 09:48:24 mungady Exp $
 *
 * Changes
 * -------
 * 20-Sep-2002 : Version 1 (DG);
 * 11-Oct-2002 : Added tooltips, modified series colors, centered frame on screen (DG);
 * 11-Feb-2003 : Fixed bug where category labels were not showing on the axis (DG);
 * 08-Sep-2003 : Bug fix (DG);
 * 02-Jun-2004 : Set the rendering order (DG);
 *
 */


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.xml.parsers.ParserConfigurationException;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.xml.sax.SAXException;

import common.configuration.Configuration;
import common.simulation.scenarios.Scenario;
import common.simulation.scenarios.Scenario2;



/**
 * A simple demonstration application showing how to create a bar chart overlaid
 * with a line chart.
 */
public class GraphPanel4 extends ApplicationFrame {

    /**
     * Default constructor.
     *
     * @param title  the frame title.
     */
	
	final JFreeChart chart;
	public JFreeChart getChart()
	{
		return chart;
	}
    public GraphPanel4(final String title, HashMap<Integer, Double> ErrorRate) {

        super(title);

        // create the first dataset...
        DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
        
        for(Integer count: ErrorRate.keySet())
        {
        	Double error = ErrorRate.get(count);

        	System.out.println("Cycle: " + count + " ErrorRate: " + error + "\n");
        	dataset1.addValue(error, "Error Rate (%)", count);

        }
        
        


        // create the first renderer...
  //      final CategoryLabelGenerator generator = new StandardCategoryLabelGenerator();
        final CategoryItemRenderer renderer = new LineAndShapeRenderer();
    //    renderer.setLabelGenerator(generator);
        renderer.setItemLabelsVisible(true);
        
        final CategoryPlot plot = new CategoryPlot();
        plot.setDataset(dataset1);
        plot.setRenderer(renderer);
        
        plot.setDomainAxis(new CategoryAxis("Cycle #"));
        plot.setRangeAxis(new NumberAxis("Error Rate (%)"));

        plot.setOrientation(PlotOrientation.VERTICAL);
        plot.setRangeGridlinesVisible(true);
        plot.setDomainGridlinesVisible(true);


        // create the third dataset and renderer...
        //final ValueAxis rangeAxis2 = new NumberAxis("Path Length");
        //plot.setRangeAxis(1, rangeAxis2);

        //DefaultCategoryDataset dataset3 = new DefaultCategoryDataset();
        //for(Integer count: PathLength.keySet())
        //{
        //	Double pl = PathLength.get(count);
        //	dataset3.addValue(pl, "Path Length", count);
        //}


        //plot.setDataset(2, dataset3);
        final CategoryItemRenderer renderer3 = new LineAndShapeRenderer();
        plot.setRenderer(2, renderer3);
        plot.mapDatasetToRangeAxis(2, 1);

        // change the rendering order so the primary dataset appears "behind" the 
        // other datasets...
        plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
        
        plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);
        chart = new JFreeChart(plot);
        chart.setTitle(title);
      //  chart.setLegend(new StandardLegend());

        // add the chart to a panel...
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        getContentPane().add(chartPanel);
        JButton submit = new JButton("Back to main menu");

        /*submit.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              String filename = "toverlay1.graphml";
              try {
				new OverlayPanel(filename);
			} catch (ParserConfigurationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (SAXException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}  
          }
        });*/
        
        JPanel controls = new JPanel();
        controls.add(submit, BorderLayout.EAST);
        getContentPane().add(controls, BorderLayout.EAST);
        setPreferredSize(new java.awt.Dimension(900, 570));

    }


    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
   /* public static void main(final String[] args) {

        final graph_panel demo = new graph_panel("Simulation Statistics");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }*/

}
